/*
 * @Author: czy
 * @Date: 2021-04-26 22:01:06
 * @LastEditTime: 2021-05-04 16:15:09
 */
#include "mystdio.h"
#include "mystdlib.h"
#include "mystring.h"
#include "mytime.h"
#include "shell.h"

int cmd(int argc, char *argv[])
{
    command commands[COMMAND_SIZE];
    int length;
    getcmd(commands, &length);
    for (int i = 0; i < length; i++)
    {
        myprintk("%s\n", commands[i].name);
    }
    return length;
}

int help(int argc, char *argv[])
{
    int i = 0;
    command cmd;
    if (argc == 1)
    {
        match("help", &cmd);
        myprintk("Usage: help [command]\nDisplay info about [command].\n");
    }
    for (int i = 1; i < argc; i++)
    {
        if (match(argv[i], &cmd))
        {
            myprintk("%s\n", cmd.help_content);
            i++;
        }
        else
        {
            myprintk("help: no help topics match `%s`.\n", argv[i]);
        }
    }
    return i;
}

int _myExit(int argc, char *argv[])
{
    int value = 0;
    if (argc > 1)
    {
        value = str2int(argv[1]);
    }
    myExit(value);
    return value;
}

int history(int argc, char *argv[])
{
    int num = -1;
    if (argc > 1)
    {
        num = str2int(argv[1]);
    }
    print_history_command(num);
    return num;
}

int date(int argc, char *argv[])
{
    char temp[100] = "";
    if (argc == 1)
    {
        mystrftime(temp, "%a %b\t%H:%M:%S %Y", gmtime());
        myprintf("%s\n", temp);
    }
    else
    {
        mystrftime(temp, argv[1], gmtime());
        myprintf("%s\n", temp);
    }
}

int echo(int argc, char *argv[])
{
    for (int i = 1; i < argc; i++)
    {
        myprintf("%s ", argv[i]);
    }
    myPutchar('\n');
    return argc - 1;
}

int _sleep(int argc, char *argv[])
{
    if (argc == 1)
    {
        myprintf("sleep: missing operand\n");
        return 0;
    }
    else
    {
        sleep(str2int(argv[1]));
        return str2int(argv[1]);
    }
}

int cal(int argc, char *argv[])
{
    char *month_str[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"};
    tm now = gmtime();
    int mday = now.tm_mday;
    if (argc == 2)
    {
        now.tm_mon = str2int(argv[1]) - 1;
    }
    if (argc > 2)
    {
        now.tm_mon = str2int(argv[1]) - 1;
        now.tm_year = str2int(argv[2]);
    }
    now.tm_mday = 1;
    int tol_day = (int)date_difference(base_date(), now);
    int weekday = (tol_day + base_date().tm_wday) % 7;
    int day = month_day(now.tm_mon, now.tm_year);
    myprintf("      %s %d\n", month_str[now.tm_mon], now.tm_year);
    myprintf("Su Mo Tu We Th Fr Sa\n");
    now.tm_mday = mday;
    for (int i = 0; i < weekday; i++)
    {
        myprintf("   ");
    }
    for (int i = 1; i <= day; i++)
    {
        if (i == now.tm_mday)
        {
            myPrintf(0xf0, "%.2d", i);
            myprintf(" ");
        } else
        {
            myprintf("%.2d ", i);
        }
        if ((i + tol_day + base_date().tm_wday - 1) % 7 == 6)
        {
            myprintf("\n");
        }
    }
    myprintf("\n");
}

/**
 * @description: Register command.
 */
void register_command(void)
{
    addcmd("help", "Usage: help [command]\nDisplay info about [command].", help);
    addcmd("cmd", "List available commands.", cmd);
    addcmd("exit", "Usage: exit [status]\nExits the shell with a status code.", _myExit);
    addcmd("history", "Usage: history [n]\nDisplay the history list.", history);
    addcmd("date", "Usage: date [FORMAT]\nDisplay the current time in the given FORMAT (Don't use spaces in FORMAT).", date);
    addcmd("cal", "Usage: cal [MONTH] [YEAR]\nDisplays the calendar.", cal);
    addcmd("echo", "Usage: echo [arg ...]\nWrite arguments to the standard output.", echo);
    addcmd("sleep", "Usage: sleep NUMBER\nPause for NUMBER seconds.", _sleep);
}
